<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller\learn\book;

use app\api\controller\Controller;
use app\api\model\learn\article\ReviewUser as TitleReviewModel;
use app\api\model\learn\Book as BookModel;
use app\api\model\learn\book\CategoryUser as CategoryUserModel;
use app\api\model\learn\book\Comment as CommentModel;
use app\api\model\learn\book\Title as ArticleModel;
use app\api\model\learn\book\TitleUser as TitleUserModel;
use app\api\service\User as UserService;
use think\facade\Db;
use think\response\Json;

/**
 * 题目
 * Class Category
 * @package app\api\controller\article
 */
class Index extends Controller {
	/**
	 * 文章分类列表
	 * @return Json
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function list($categoryId = 0): Json {

		$model = new ArticleModel;
		$list = $model->getShowList($categoryId, $this->request->param());
		return $this->renderSuccess(compact('list'));
	}
	//我的条文列表
	public function todayList(int $type, int $bookId): Json {
		$model = new TitleUserModel;

		$list = $model->getList($type, $bookId);
		return $this->renderSuccess(compact('list'));
	}
	public function detail(int $articleId = 0, int $bookId = 0): Json {
		$userId = UserService::getCurrentLoginUserId();
		if ($articleId > 0) {
			$detail = TitleUserModel::getDetail(['article_id' => $articleId, 'user_id' => $userId]);
		} else {
			$userArticle = Db::name('learn_book_user')->where('book_id', $bookId)->select()->toArray();
			$name = Db::name('learn_book_category')->where('category_id', $bookId)->value('name');
			$ids = array_column($userArticle, 'article_id');

			$articleId = Db::name('learn_book')->where('book_name', $name)
				->where('subject_id', 'not in', $ids)
				->value('subject_id');

			$detail = ArticleModel::getDetail($articleId);
		}

		//评论数
		$detail['comment'] = Db::name('learn_book_comment')->where('article_id', $articleId)->count();

		$detail['total_num'] = Db::name('learn_book_user')->where('book_id', $detail['book_id'])->where('user_id', $userId)
			->where('book_name2', $detail['book_name2'])->count();

		$detail['now_num'] = Db::name('learn_book_category_user')
			->where('category_id', $bookId)->where('user_id', $userId)->value('complate_num');
		$is_collect = 0;
		$user_book = Db::name('learn_book_user_collect')->where('user_id', $userId)->where('article_id', $articleId)->find();
		$detail['is_collect'] = $user_book ? 1 : $is_collect;

		//我的评论数
		$detail['user_comment'] = Db::name('learn_book_comment')->where('user_id', $userId)->where('article_id', $articleId)->count();

		//我的笔记数
		$detail['user_note'] = Db::name('learn_book_note')->where('user_id', $userId)->where('article_id', $articleId)->count();

		//今日学习时间
		$time = Db::name('learn_book_user')
			->where('user_id', $userId)->where('book_id', $bookId)->sum('time');
		$hour = round($time / 3600);

		$second = $time % 3600;
		$minute = round($second / 60);

		$time = $hour > 0 ? $hour . 'H' : '';
		$time .= $minute . 'min';
		$detail['time'] = $time;

		return $this->renderSuccess(compact('detail'));
	}

	//学习计划
	public function planList() {
		$userId = UserService::getCurrentLoginUserId();
		$model = new CategoryUserModel();

		$waitList = $model->getListWait($userId);
		$complateList = $model->getListComplate($userId);

		return $this->renderSuccess(compact('waitList', 'complateList'));
	}
	//用户条文详情
	public function userBook(int $bookId) {
		$userId = UserService::getCurrentLoginUserId();
		$model = new CategoryUserModel();

		$info = $model->bookInfo($userId, $bookId);

		return $this->renderSuccess(compact('info'));
	}
	//用户设置页面
	public function setting(int $bookId) {
		$userId = UserService::getCurrentLoginUserId();
		$model = new BookModel();
		$CategoryUserModel = new CategoryUserModel();

		$info = $model->detail($bookId);

		$list = $CategoryUserModel->setting($userId, $bookId);
		$total_num_all = 0;
		$suc_num_total = 0;
		$fail_num_total = 0;
		foreach ($list as $key => $value) {
			$suc_num = Db::name('learn_book_user')
				->where('category_id', $value['category_id'])
				->where('user_id', $userId)
				->where('is_right', 1)->count();
			$fail_num = Db::name('learn_book_user')
				->where('category_id', $value['category_id'])
				->where('user_id', $userId)
				->where('is_right', 2)->count();
			$num = Db::name('learn_book_user')
				->where('category_id', $value['category_id'])
				->where('user_id', $userId)
				->where('is_right', 0)->count();
			$total_num = $suc_num + $fail_num + $num;
			if ($total_num == 0) {
				$total_num = Db::name('learn_book')
					->where('book_name2', $value['name'])
					->where('book_name', $info['name'])
					->count();
			}

			$list[$key]['suc_percent'] = $total_num > 0 ? round($suc_num / $total_num, 2) * 100 : 0;
			$list[$key]['fail_percent'] = $total_num > 0 ? round($fail_num / $total_num, 2) * 100 : 0;
			$list[$key]['total_num'] = $total_num;
			$list[$key]['complate_num'] = $suc_num + $fail_num;

			$suc_num_total += $suc_num;
			$fail_num_total += $fail_num;
			$total_num_all += $total_num;

		}
		$info['suc_percent'] = $total_num_all > 0 ? round($suc_num_total / $total_num_all, 2) * 100 : 0;
		$info['fail_percent'] = $total_num_all > 0 ? round($fail_num_total / $total_num_all, 2) * 100 : 0;
		$info['complate_num'] = $suc_num_total + $fail_num_total;
		$info['total_num'] = $total_num_all;

		return $this->renderSuccess(compact('list', 'info'));
	}
	//章节题目
	public function getCategoryTimu() {
		$userId = UserService::getCurrentLoginUserId();
		$model = new CategoryUserModel();

		$info = $model->getCategoryTimu($userId, $this->request->param());

		return $this->renderSuccess(compact('info'));
	}

	//用户学习，获取当前题目
	public function getTimu(int $bookId) {
		$userId = UserService::getCurrentLoginUserId();
		$model = new CategoryUserModel();

		$info = $model->getTimu($userId, $bookId);

		return $this->renderSuccess(compact('info'));
	}
	//用户学习，获取下一题
	public function getNextId(int $articleId, int $bookId, int $review = 0) {
		$userId = UserService::getCurrentLoginUserId();
		if ($review == 1) {
			$articleId = Db::name('learn_article_title_review')->where('book_id', $bookId)
				->where('user_id', $userId)->order('log_id asc')->value('article_id');
			$articleId = $articleId ? $articleId : 0;
		} else {
			$articleId = Db::name('learn_article_title_user')->where('book_id', $bookId)
				->where('user_id', $userId)->where('article_id', '>', $articleId)->value('article_id');
			$articleId = $articleId ? $articleId : 0;
		}

		return $this->renderSuccess(compact('articleId'));
	}
	//复习条文列表
	public function reviewList(int $bookId) {
		$userId = UserService::getCurrentLoginUserId();
		$model = new TitleUserModel();

		$list = $model->getReviewList($userId, $bookId);

		return $this->renderSuccess(compact('list'));
	}
	//保存答案
	public function answer() {
		$userId = UserService::getCurrentLoginUserId();
		$model = TitleUserModel::getDetail(['user_id' => $userId, 'article_id' => $this->postForm()['article_id']]);

		$info = $model->answer_save($userId, $this->postForm());

		return $this->renderSuccess(compact('info'));
	}
	//收藏
	public function collectSave(int $articleId, int $bookId, int $is_collect) {
		$userId = UserService::getCurrentLoginUserId();
		$model = new TitleUserModel;

		$model->collectSave($userId, $articleId, $bookId, $is_collect);

		return $this->renderSuccess();
	}
	//我认识
	public function handRen() {
		$userId = UserService::getCurrentLoginUserId();
		$model = TitleUserModel::detail(['user_id' => $userId, 'article_id' => $this->postForm()['articleId']]);

		$info = $model->handren($this->postForm());

		$model2 = new TitleReviewModel;

		$model2->add($userId, $this->postForm());

		return $this->renderSuccess();
	}
	//标志简单
	public function handEasy() {
		$userId = UserService::getCurrentLoginUserId();
		$model = TitleUserModel::detail(['user_id' => $userId, 'article_id' => $this->postForm()['articleId']]);
		if ($model['is_easy'] == 0) {
			$model->handEasy();
		}

		return $this->renderSuccess();
	}

	//评论
	public function commentSave() {

		$model = new CommentModel;

		$model->increased($this->postForm());

		return $this->renderSuccess();
	}
}